/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.DaftarKelasDAO;
import com.ivanbiz.dao.GLAccountDAO;
import com.ivanbiz.dao.InvoiceDAO;
import com.ivanbiz.dao.PerusahaanDAO;
import com.ivanbiz.dao.impl.DaftarKelasDAOImpl;
import com.ivanbiz.dao.impl.GLAccountDAOImpl;
import com.ivanbiz.dao.impl.InvoiceDAOImpl;
import com.ivanbiz.dao.impl.PerusahaanDAOImpl;
import com.ivanbiz.model.Bank;
import com.ivanbiz.model.DaftarKelas;
import com.ivanbiz.model.GLAccount;
import com.ivanbiz.model.Invoice;
import com.ivanbiz.model.Kelas;
import com.ivanbiz.model.Murid;
import com.ivanbiz.model.Perusahaan;
import com.ivanbiz.report.GlobalReport;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import org.zkoss.util.media.AMedia;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Iframe;

/**
 *
 * @author IW20149X
 */
public class TagihanLaporanViewService extends SelectorComposer<Component> {

    @Wire
    Iframe ipdf;
    InvoiceDAO invoiceDAO;
    DaftarKelasDAO daftarKelasDAO;
    SimpleDateFormat simpleDateFormat;
    SimpleDateFormat simpleDateFormatFrom;
    PerusahaanDAO perusahaanDAO;
    GLAccountDAO gLAccountDAO;
    NumberFormat numberFormat;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        invoiceDAO = new InvoiceDAOImpl();
        daftarKelasDAO = new DaftarKelasDAOImpl();
        gLAccountDAO = new GLAccountDAOImpl();
        numberFormat = NumberFormat.getNumberInstance();

        Invoice invoice = (Invoice) Executions.getCurrent().getArg().get("invoice");

        Kelas kelas = invoice.getKelas();
        Bank bank = invoice.getBank();

        List<DaftarKelas> listDaftarKelas = daftarKelasDAO.getDataByEqualsMore(kelas, bank);

        perusahaanDAO = new PerusahaanDAOImpl();
        List<Perusahaan> listPerusahaan = perusahaanDAO.getAll(Perusahaan.class);
        Perusahaan perusahaan = listPerusahaan.get(0);
        perusahaan.setAlamat(perusahaan.getAlamat() + "\n" + "Ph  :" + perusahaan.getTelephone() + "\n" + "Fax :" + perusahaan.getFax());

        invoice.setNII("NO. INV : " + invoice.getNII());

        String[] kepada = invoice.getDeskripsiKepada().split("#");
        GLAccount gLAccountKepada = (GLAccount) gLAccountDAO.getDataByEqual(GLAccount.class, "noGL", kepada[0]);
        invoice.setDeskripsiKepada(gLAccountKepada.getDeskripsi());

        String[] ditransferUntuk = invoice.getDeskripsiUntuk().split("#");
        GLAccount gLAccountDitransferKe = (GLAccount) gLAccountDAO.getDataByEqual(GLAccount.class, "noGL", ditransferUntuk[0]);
        invoice.setDeskripsiUntuk(gLAccountDitransferKe.getDeskripsi() + "\nA/C No. " + gLAccountDitransferKe.getNoGL());

        invoice.setDeskripsiJumlahPeserta(invoice.getDeskripsiJumlahPeserta() + " Orang Peserta, yaitu :\n \n ( absensi terlampir )");
        invoice.setTerbilang(ServiceHelper.bilang(Integer.parseInt(String.valueOf(new Double(invoice.getJumlahTagihan()).intValue()))) + " rupiah");
        invoice.setJatuhTempo(invoice.getJatuhTempo() + " hari");

        GlobalReport globalReport = new GlobalReport();
        globalReport.setPerusahaan(perusahaan);
        globalReport.setInvoice(invoice);
        globalReport.setNamaGLDebitur(gLAccountKepada.getNameGL());
        globalReport.setNamaGLKreditur(gLAccountDitransferKe.getNameGL());
        globalReport.setLogo(Executions.getCurrent().getDesktop().getWebApp().getRealPath("/img/logo.jpg"));
        globalReport.setJumlah("Rp " + numberFormat.format(invoice.getJumlahTagihan()) + ",00");

        List<GlobalReport> listReport = new ArrayList<GlobalReport>();
        listReport.add(globalReport);

        List<DaftarKelas> listDaftarKelases = new ArrayList<DaftarKelas>();
        for (DaftarKelas daftarKelas : listDaftarKelas) {
            Murid murid = daftarKelas.getMurid();
            murid.setTelp("Telp : " + murid.getTelp() + "\n" + " HP   : " + murid.getHandphone());
            daftarKelas.setMurid(murid);
            kelas = daftarKelas.getKelas();
            listDaftarKelases.add(daftarKelas);
        }

        String reportFile = Executions.getCurrent().getDesktop().getWebApp().getRealPath("/report/TagihanReport.jasper");
        String subReportFile = Executions.getCurrent().getDesktop().getWebApp().getRealPath("/report/DaftarKelasReport.jasper");

        Map map = new HashMap();
        map.put("SUBREPORT_DIR", subReportFile);
        map.put("PRM_DETAIL_VALUE", listDaftarKelases);
        map.put("logo", globalReport.getLogo());
        map.put("perusahaan.alamat", globalReport.getPerusahaan().getAlamat());
        map.put("kelas.alamatKelas", globalReport.getInvoice().getKelas().getAlamatKelas() + "\n" + "bertempat di : " + globalReport.getInvoice().getKelas().getTempatKelas());
        map.put("peserta", listDaftarKelas.size());

        JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile, map, new JRBeanCollectionDataSource(listReport));
        loadToStream(jasperPrint);
    }

    private void loadToStream(JasperPrint jasperPrint) throws JRException, IOException {
        ByteArrayOutputStream bytesOutputStream = new ByteArrayOutputStream();
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(bytesOutputStream);

        JRExporter exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, bufferedOutputStream);
        exporter.exportReport();

        InputStream mediais = new ByteArrayInputStream(bytesOutputStream.toByteArray());
        bytesOutputStream.close();

        AMedia amedia = new AMedia(jasperPrint.getName() + ".pdf", "pdf", "application/pdf", mediais);
        ipdf.setContent(amedia);
    }
}
